package com.example.syl.basaimovie;



import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageButton tuijianIB,fenleiIB,findIB,userIB;
    private RecommendFragment tuiJianFragment;
    private SortFragment fenLeiFragment;
    private FindFragment findFragment;
    private UserFragment userFragment;
    private FragmentManager manager;
    private FragmentTransaction transaction;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        //初始化控件
        initView();
        //控件的点击事件
        initViewEvent();
        //选中fragment
        setSelect(0);


    }


    //初始化控件
    private void initView() {
        tuijianIB= (ImageButton) findViewById(R.id.tuijianImage);
        fenleiIB= (ImageButton) findViewById(R.id.fenleiImage);
        findIB= (ImageButton) findViewById(R.id.findImage);
        userIB= (ImageButton) findViewById(R.id.userImage);
    }
    private void initViewEvent() {
        tuijianIB.setOnClickListener(this);
        fenleiIB.setOnClickListener(this);
        findIB.setOnClickListener(this);
        userIB.setOnClickListener(this);
    }


    public void setSelect(int i) {
        clearImageButton();
        manager=getSupportFragmentManager();
        transaction=manager.beginTransaction();
        hideFragment(transaction);
        switch (i){
            case 0://推荐
                tuijianIB.setImageResource(R.mipmap.tab_home_d);
                if (tuiJianFragment==null){
                    tuiJianFragment=new RecommendFragment();
                    transaction.add(R.id.fragment_ui,tuiJianFragment);
                }else {
                    transaction.show(tuiJianFragment);
                }
                break;
            case 1://分类
                fenleiIB.setImageResource(R.mipmap.tab_theme_d);
                if (fenLeiFragment==null){
                    fenLeiFragment=new SortFragment();
                    transaction.add(R.id.fragment_ui,fenLeiFragment);
                }else {
                    transaction.show(fenLeiFragment);
                }
                break;
            case 2://发现
                findIB.setImageResource(R.mipmap.tab_find_d);
                if (findFragment==null){
                    findFragment=new FindFragment();
                    transaction.add(R.id.fragment_ui,findFragment);
                }else {
                    transaction.show(findFragment);
                }
                break;
            case 3://用户
                userIB.setImageResource(R.mipmap.tab_user_d);
                if (userFragment==null){
                    userFragment=new UserFragment();
                    transaction.add(R.id.fragment_ui,userFragment);
                }else {
                    transaction.show(userFragment);
                }
                break;
            default:
                break;
        }
        transaction.commit();
    }

    //隐藏fragment
    private void hideFragment(FragmentTransaction transaction2) {
        if (tuiJianFragment!=null){
            transaction2.hide(tuiJianFragment);
        }
        if (fenLeiFragment!=null){
            transaction2.hide(fenLeiFragment);
        }
        if (findFragment!=null){
            transaction2.hide(findFragment);
        }
        if (userFragment!=null){
            transaction2.hide(userFragment);
        }
    }

    private void clearImageButton() {
        tuijianIB.setImageResource(R.mipmap.tab_home);
        fenleiIB.setImageResource(R.mipmap.tab_theme);
        findIB.setImageResource(R.mipmap.tab_find);
        userIB.setImageResource(R.mipmap.tab_user);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tuijianImage://推荐
                setSelect(0);
                break;
            case R.id.fenleiImage://分类
                setSelect(1);
                break;
            case R.id.findImage://发现
                setSelect(2);
                break;
            case R.id.userImage://用户
                setSelect(3);
                break;
            default:
                break;

        }
    }
}
